package com.bskj.bms.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bskj.bms.bean.req.BasePageListReqVo;
import com.bskj.bms.bean.req.BillListReqVo;
import com.bskj.bms.bean.req.ShopListReqVo;
import com.bskj.bms.bean.vo.ShopCouponVo;
import com.bskj.bms.bean.vo.UserCouponReleaseRespVo;
import com.bskj.bms.service.IBmsCouponService;
import com.bskj.bms.service.IBmsShopService;
import com.bskj.common.bean.model.AggregateAmountsVo;
import com.bskj.common.bean.model.BmsUser;
import com.bskj.common.bean.model.FindSumChargeVo;
import com.bskj.common.bean.model.ParkingShop;
import com.bskj.common.bean.model.ShopCoupon;
import com.bskj.common.bean.model.ShopCouponLog;
import com.bskj.common.bean.model.UserCoupon;
import com.bskj.common.dao.ParkInfoDao;
import com.bskj.common.dao.ParkingShopDao;
import com.bskj.common.dao.ShopCouponDao;
import com.bskj.common.dao.ShopCouponLogDao;
import com.bskj.common.dao.UserCouponDao;
import com.bskj.common.dao.core.BaseDao;
import com.bskj.common.dao.core.FindRequestEntity;
import com.bskj.common.dao.core.Page;
import com.bskj.common.dao.core.WhereEnum;
import com.bskj.common.enums.CouponLimitModeTypeEnum;
import com.bskj.common.enums.CouponStatusEnum;
import com.bskj.common.service.impl.BaseServiceImpl;
import com.bskj.common.utils.LogUtils;

@Service
public class BmsCouponServiceImpl extends BaseServiceImpl<ShopCoupon> implements IBmsCouponService {

	@Autowired
	private ShopCouponDao shopCouponDao;

	@Autowired
	private UserCouponDao userCouponDao;

	@Autowired
	private IBmsShopService bmsShopService;

	@Autowired
	private ParkInfoDao parkInfoDao;
	
	@Autowired
	private ShopCouponLogDao shopCouponLogDao;
	
	@Autowired
	private ParkingShopDao parkingShopDao;

	@Override
	public BaseDao<ShopCoupon> getBaseDao() {
		return shopCouponDao;
	}

	@Override
	public Page<ShopCouponVo> findAllCoupon(Integer custId, BasePageListReqVo req) throws Exception {
		FindRequestEntity findReq = new FindRequestEntity();
		findReq.addWhere("parking_lot_customer_id", WhereEnum.EQ, custId);
		findReq.addWhere("shop_id", WhereEnum.EQ, req.getShopId());
		findReq.setPage(req.getPageNo(), req.getPageSize());
		Page<ShopCoupon> shopCouponPage = page(findReq);
		List<ShopCoupon> shopCouponList = shopCouponPage.getList();

		Page<ShopCouponVo> shopCouponVoPage = new Page<>();
		List<ShopCouponVo> shopCouponVoList = new ArrayList<>();
		for (ShopCoupon shopCoupon2 : shopCouponList) {
			ShopCouponVo shopCouponVo = new ShopCouponVo();
			BeanUtils.copyProperties(shopCoupon2, shopCouponVo, "insertMap", "", "");
			shopCouponVoList.add(shopCouponVo);
		}

		shopCouponVoPage.setPage(shopCouponPage.getPage());
		shopCouponVoPage.setPageSize(shopCouponPage.getPageSize());
		shopCouponVoPage.setTotalRow(shopCouponPage.getTotalRow());
		shopCouponVoPage.setList(shopCouponVoList);

		return shopCouponVoPage;
	}

	@Override
	public void addCoupon(ShopCouponVo req, Integer custId) throws Exception {
		ShopCoupon shopCoupon = new ShopCoupon();
		shopCoupon.setMoney(req.getMoney());
		shopCoupon.setValidType(req.getValidType());
		shopCoupon.setValidTime(req.getValidTime());
		shopCoupon.setLastTime(req.getLastTime());
		shopCoupon.setParkingLotCustomerId(custId);
		shopCoupon.setStatus(1);
		shopCoupon.setCouponMoneyType(req.getCouponMoneyType());
		shopCoupon.setShopId(req.getShopId());
		shopCoupon.setCreateTime(new Date());
		shopCoupon.setUpdateTime(new Date());
		shopCouponDao.insert(shopCoupon);
	}

	@Override
	public Page<UserCouponReleaseRespVo> shopsCoupon(BillListReqVo req, Integer custId) throws Exception {
		FindRequestEntity findReq = new FindRequestEntity();
		findReq.addWhere("parking_lot_customer_id", WhereEnum.EQ, custId);
		ShopListReqVo shopListReqVo = new ShopListReqVo();
		shopListReqVo.setCustId(custId);
		shopListReqVo.setName(req.getEntNo());
		shopListReqVo.setPageSize(req.getPageSize());
		shopListReqVo.setStartIndex(req.getStartIndex());
		Page<ParkingShop> parkingShops = bmsShopService.findAllShop(shopListReqVo);
		List<ParkingShop> parkingShopList = parkingShops.getList();
		List<UserCouponReleaseRespVo> userCouponReleaseRespVoList = new ArrayList<>();
		for (ParkingShop parkingShop : parkingShopList) {
			userCouponReleaseRespVoList.add(shopCouponTo(req, parkingShop));
		}
		Page<UserCouponReleaseRespVo> userCouponReleaseRespVos = new Page<>();
		userCouponReleaseRespVos.setList(userCouponReleaseRespVoList);
		userCouponReleaseRespVos.setPage(parkingShops.getPage());
		userCouponReleaseRespVos.setPageSize(parkingShops.getPageSize());
		userCouponReleaseRespVos.setTotalRow(parkingShops.getTotalRow());
		return userCouponReleaseRespVos;
	}

	@Override
	public List<UserCouponReleaseRespVo> shopCoupon(BillListReqVo req, Integer shopId) throws Exception {
		List<UserCouponReleaseRespVo> userCouponReleaseRespVos = new ArrayList<>();
		ParkingShop parkingShop = bmsShopService.get(shopId);
		if (parkingShop != null) {
			userCouponReleaseRespVos.add(shopCouponTo(req, parkingShop));
		}
		return userCouponReleaseRespVos;
	}

	private UserCouponReleaseRespVo shopCouponTo(BillListReqVo req, ParkingShop parkingShop) throws Exception {
		UserCouponReleaseRespVo userCouponReleaseRespVo = new UserCouponReleaseRespVo();
		FindRequestEntity findReq = new FindRequestEntity();
		findReq.addWhere("shop_id", WhereEnum.EQ, parkingShop.getId());
		List<ShopCoupon> shopCoupons = shopCouponDao.find(findReq);
		for (ShopCoupon shopCoupon : shopCoupons) {
			Integer count = null;
			count = userCouponDao.getDailyTotal(parkingShop.getParkingLotCustomerId() + "", req.getStartTime(),
					req.getEndTime(), shopCoupon.getId());

			int totalMoney = 0;
			findReq = new FindRequestEntity();
			if (req.getStartTime() != null) {
				findReq.addWhere("create_time", WhereEnum.GT, req.getStartTime());
			}
			if (req.getEndTime() != null) {
				findReq.addWhere("create_time", WhereEnum.LT, req.getEndTime());
			}
			findReq.addWhere("coupon_id", WhereEnum.EQ, shopCoupon.getId());
			findReq.addWhere("status", WhereEnum.EQ, CouponStatusEnum.used.getKey());
			List<UserCoupon> userCouponList = userCouponDao.find(findReq);
			if (userCouponList != null && userCouponList.size() > 0) {
				List<Integer> userCouponIdList = new ArrayList<>();
				for (UserCoupon userCoupon : userCouponList) {
					userCouponIdList.add(userCoupon.getId());
				}
				FindSumChargeVo findSumChargeVo = new FindSumChargeVo();
				findSumChargeVo.setCustId(userCouponIdList);
				findSumChargeVo.setStartTime(req.getStartTime());
				findSumChargeVo.setEndTime(req.getEndTime());
				AggregateAmountsVo agg = parkInfoDao.getSumCoupon(findSumChargeVo);
				totalMoney = agg.getCount() - agg.getSum();

			}
			userCouponReleaseRespVo.setTotalMsg(shopCoupon.getCouponMoneyType(), userCouponReleaseRespVo.getTotalMsg(),
					shopCoupon.getMoney(), userCouponList.size(), totalMoney, count);
		}
		userCouponReleaseRespVo.setParkingShopName(parkingShop.getName());
		return userCouponReleaseRespVo;
	}

	@Override
	public Map<String, Object> setShopCouponLimit(ShopCoupon shopCoupon, BmsUser bmsUser) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("result", true);
		resultMap.put("errorMsg", "修改成功");
		ShopCoupon originShopCoupon = get(shopCoupon.getId());
		if (originShopCoupon == null) {
			resultMap.put("result", false);
			resultMap.put("errorMsg", "停车券不存在！");
			return resultMap;
		}

		CouponLimitModeTypeEnum modeType = CouponLimitModeTypeEnum.get(shopCoupon.getLimitModeType());
		if (modeType == null) {
			resultMap.put("result", false);
			resultMap.put("errorMsg", "停车券发放限制规则不合法！");
			return resultMap;
		}

		ShopCouponLog shopCouponLog = new ShopCouponLog();

		ShopCouponVo originShopCoupnVo = new ShopCouponVo();
		BeanUtils.copyProperties(originShopCoupon, originShopCoupnVo);
		ParkingShop shop = parkingShopDao.get(originShopCoupon.getShopId());
		if(shop != null) {
			shopCouponLog.setShopName(shop.getName());
		}
		shopCouponLog.setCouponId(originShopCoupon.getId());
		shopCouponLog.setShopId(originShopCoupon.getShopId());
		shopCouponLog.setParkingLotCustomerId(originShopCoupon.getParkingLotCustomerId());
		shopCouponLog.setOriginValue(originShopCoupnVo.getCouponLimit());
		shopCouponLog.setOperator(bmsUser.getEntNo());
		shopCouponLog.setOperatorId(bmsUser.getId());
		shopCouponLog.setCouponDesc(originShopCoupnVo.getCouponRule() +  "的" + originShopCoupnVo.getMoneys() + "券");

		originShopCoupon.setLimitModeType(shopCoupon.getLimitModeType());
		originShopCoupon.setUpdateTime(new Date());
		if (CouponLimitModeTypeEnum.Time_Number_Type.equals(modeType)) {
			originShopCoupon.setCouponLimitType(shopCoupon.getCouponLimitType());
			originShopCoupon.setCouponLimitNum(shopCoupon.getCouponLimitNum());
			shopCouponLog.setActionDesc("调整");
		} else if (CouponLimitModeTypeEnum.Inventory_Type.equals(modeType)) {
			int originShopCouponInventory = originShopCoupon.getInventory() == null ? 0
					: originShopCoupon.getInventory();
			int newInventory = originShopCouponInventory + shopCoupon.getInventory();
			originShopCoupon.setInventory(newInventory);

			shopCouponLog.setActionDesc(String.format("增加库存%s", shopCoupon.getInventory()));
		}

		ShopCouponVo newShopCoupnVo = new ShopCouponVo();
		BeanUtils.copyProperties(originShopCoupon, newShopCoupnVo);
		shopCouponLog.setNewValue(newShopCoupnVo.getCouponLimit());

		update(originShopCoupon);

		shopCouponLog.setCreateTime(new Date());
		shopCouponLog.setUpdateTime(new Date());
		try {
			shopCouponLogDao.insert(shopCouponLog);
		} catch (Exception e) {
			e.printStackTrace();
			LogUtils.error("记录停车券修改规则日志失败！");
		}
		return resultMap;
	}
}
